﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using edu.Model;
using System.Data;
using Discuz.Data;
using System.Data.Common;
using System.Data.SqlClient;
using edu.Comm;

namespace edu.Service
{
    public class DailyHomeworkService
    {
        public static int Add(DailyHomework entity)
        {
            int id;
            string sql = @"insert into edu_dailyHomework([Code],[Name],[Description],[HomeworkId],[StudentId],[HomeworkCompleteId]) 
                        values(@Code,@Name,@Description,@HomeworkId,@StudentId,@HomeworkCompleteId)";
            DbParameter[] param ={
                new SqlParameter("Code", entity.Code),
                new SqlParameter("Name",entity.Name),
                new SqlParameter("Description",entity.Description),
                new SqlParameter("HomeworkId",entity.HomeworkId),
                new SqlParameter("StudentId", entity.StudentId),
                new SqlParameter("HomeworkCompleteId",entity.HomeworkCompleteId)
                                };
            DbHelper.ExecuteNonQuery(out id, CommandType.Text, sql, param);
            return id;
        }

        public static void Save(IList<DailyHomework> list)
        {
            //筛选出更新和新增的记录
            List<DailyHomework> addList = list.Where(c => c.Id == 0).ToList();
            List<DailyHomework> updateList = list.Where(c => c.Id != 0).ToList();

            //新增
            foreach (var i in addList)
            {
                Add(i);
            }

            //修改
            foreach (var i in updateList)
            {
                Update(i);
            }
        }

        public static void Update(DailyHomework entity)
        {
            string sql = @"update edu_DailyHomework set [Code]=@Code,[Name]=@Name,[Description]=@Description,
                            [HomeworkId]=@HomeworkId,[StudentId]=@StudentId,[HomeworkCompleteId]=@HomeworkCompleteId
                            where Id=@Id";

            DbParameter[] param ={
                new SqlParameter("Code", entity.Code),
                new SqlParameter("Name",entity.Name),
                new SqlParameter("Description",entity.Description),
                new SqlParameter("HomeworkId",entity.HomeworkId),
                new SqlParameter("StudentId", entity.StudentId),
                new SqlParameter("HomeworkCompleteId",entity.HomeworkCompleteId),
                new SqlParameter("Id",entity.Id)
                                };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static void Update(IList<DailyHomework> list)
        {
            throw new NotImplementedException();
        }

        public static void Delete(DailyHomework entity)
        {
            throw new NotImplementedException();
        }

        public static void Delete(int id)
        {
            string sql = string.Format("delete from edu_dailyhomework where Id=@Id");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Id", Value=id}
                              };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static DataTable GetHomeWorkList(int classId,int year,int month,int day)
        {
            string sql = @"select * from v_homework where classId = @classId 
                            and year=@year and month=@month and day=@day
                            order by student ,course ";
            DbParameter[] paras ={ new SqlParameter("classId", classId),
                                    new SqlParameter("year", year),
                                    new SqlParameter("month", month),
                                    new SqlParameter("day", day)};
            return DbHelper.ExecuteDataset(CommandType.Text, sql, paras).Tables[0];
        }

        public static DataTable GetHomeWorkList(int classId, int courseId,int year, int month, int day)
        {
            string sql = @"select * from v_homework where classId = @classId and courseId=@courseId
                            and year=@year and month=@month and day=@day
                            order by student ";
            DbParameter[] paras ={ new SqlParameter("classId", classId),
                                    new SqlParameter("courseId", courseId),
                                    new SqlParameter("year", year),
                                    new SqlParameter("month", month),
                                    new SqlParameter("day", day)};
            DataTable table = DbHelper.ExecuteDataset(CommandType.Text, sql, paras).Tables[0];

            return table;
        }

        public static DataTable GetHomeWorkListByStudentId(int studentId,SpanType spanType)
        {
            DateTime start,end;
            switch (spanType)
            {
                case SpanType.Day:
                    start = DateTime.Parse(DateTime.Now.ToShortDateString() + " 00:00:00");
                    end = DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59");
                    break;
                case SpanType.Week:
                    int dayOfWeek = (int)DateTime.Now.DayOfWeek;
                    if (dayOfWeek == 0)
                        dayOfWeek = 7;
                    start = DateTime.Parse(DateTime.Now.AddDays(1 - dayOfWeek).ToShortDateString() + " 00:00:00");
                    end = DateTime.Parse(DateTime.Now.AddDays(7 - dayOfWeek).ToShortDateString() + " 23:59:59");
                    break;
                case SpanType.Month:
                    start = DateTime.Parse(DateTime.Now.AddDays(1 - DateTime.Now.Day).ToShortDateString() + " 00:00:00");
                    end = DateTime.Parse(DateTime.Now.AddDays(DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month) 
                        - DateTime.Now.Day).ToShortDateString() + " 23:59:59");
                    break;
                default:
                    return null;
            }

            return GetHomeWorkListByStudentId(studentId, start, end);
        }

        public static DataTable GetHomeWorkListByStudentId(int studentId, DateTime start,DateTime end)
        {
            string sql = @"select * from v_homework where studentId = @studentId and shortDate between @start and @end 
                            order by shortdate,course";
            List<DbParameter> paras = new List<DbParameter>();
            paras.Add(new SqlParameter("studentId", studentId));
            paras.Add(new SqlParameter("start", start));
            paras.Add(new SqlParameter("end", end));

            return DbHelper.ExecuteDataset(CommandType.Text, sql, paras.ToArray()).Tables[0];
        }

    }
}
